package cn.edu.zjut.kelvin.dao;
// default package

import java.sql.Blob;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;

import cn.edu.zjut.kelvin.reader.Reader;
import cn.edu.zjut.kelvin.sessionfactory.DestHibernateSessionFactory;

/**
 	* A data access object (DAO) providing persistence and search support for Reader entities.
 			* Transaction control of the save(), update() and delete() operations 
		can directly support Spring container-managed transactions or they can be augmented	to handle user-managed Spring transactions. 
		Each of these methods provides additional information for how to configure it for the desired type of transaction control. 	
	 * @see .Reader
  * @author MyEclipse Persistence Tools 
 */

public abstract class ReaderDAO implements DAOInterface  {
	protected static final Log log = LogFactory.getLog(ReaderDAO.class);
	//property constants
	public static final String RD_BARCODE = "rdBarcode";
	public static final String RD_NAME = "rdName";
	public static final String RD_SEX = "rdSex";
	public static final String RD_ORANK = "rdORank";
	public static final String RD_DEPARTMENT = "rdDepartment";
	public static final String RD_SPECIALITY = "rdSpeciality";
	public static final String RD_GRADE = "rdGrade";
	public static final String RD_TELEPHONE = "rdTelephone";
	public static final String RD_MOBILEPHONE = "rdMobilephone";
	public static final String RD_ADDRESS = "rdAddress";
	public static final String RD_DATE_BORN = "rdDateBorn";
	public static final String RD_DATE_RPLOSS = "rdDateRploss";
	public static final String RD_SUSP_AS = "rdSuspAs";
	public static final String RD_DATE_USELESS = "rdDateUseless";
	public static final String RD_DATE_GET = "rdDateGet";
	public static final String RD_MOMENY = "rdMomeny";
	public static final String RD_EMAIL = "rdEmail";
	public static final String RD_PASSWORD = "rdPassword";
	public static final String ILL = "ill";
	public static final String RD_POSITION = "rdPosition";
	public static final String RD_JJDATE = "rdJjdate";
	public static final String RD_DEPOSIT = "rdDeposit";
	public static final String PARA = "para";
	public static final String RD_CLASSNUM = "rdClassnum";
	public static final String RD_BODYCODE = "rdBodycode";
	public static final String ZX = "zx";
	public static final String XGH = "xgh";
	public static final String CARDCODE = "cardcode";
	public static final String PERCODE = "percode";

    public void save(Reader transientInstance) {
        log.debug("saving Reader instance");
        Transaction tran=getSession().beginTransaction();
        try {
        	String s= (String) getSession().save(transientInstance);
        	System.out.println("s=" + s);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
        tran.commit();
    }
    
    public void save(List<Reader> transientInstances) {
        log.debug("saving Reader instances");
        Transaction tran=getSession().beginTransaction();
        try {
        	for (Reader r: transientInstances){
        		try{
        			getSession().save(r);
        		}catch (Exception e){
        			log.debug("Reader number:" + r.getRdNumber() + "ignored");
        			continue;
        		}
        	}
            log.debug("all save successful");
        	getSession().flush();// 原因参照:
        	getSession().clear();// http://www.cnblogs.com/xuwenyan/archive/2008/02/18/1072795.html
        } catch (RuntimeException re) {
            log.error("some save failed", re);
            throw re;
        }
        tran.commit();

    }
    
    public void delete(Reader persistentInstance) {
        log.debug("deleting Reader instance");
        try {
            getSession().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
    
    public Reader findById( java.lang.String id) {
        log.debug("getting Reader instance with id: " + id);
        try {
            Reader instance = (Reader) getSession()
                    .get("Reader", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
    
    public List findByExample(Reader instance) {
        log.debug("finding Reader instance by example");
        try {
            List results = getSession()
                    .createCriteria("Reader")
                    .add(Example.create(instance))
            .list();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    /*
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding Reader instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from Reader as model where model." 
         						+ propertyName + "= ?";
         Query queryObject = getSession().createQuery(queryString);
		 queryObject.setParameter(0, value);
		 return queryObject.list();
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}
     
	public List findByRdBarcode(Object rdBarcode
	) {
		return findByProperty(RD_BARCODE, rdBarcode
		);
	}
	
	public List findByRdName(Object rdName
	) {
		return findByProperty(RD_NAME, rdName
		);
	}
	
	public List findByRdSex(Object rdSex
	) {
		return findByProperty(RD_SEX, rdSex
		);
	}
	
	public List findByRdORank(Object rdORank
	) {
		return findByProperty(RD_ORANK, rdORank
		);
	}
	
	public List findByRdDepartment(Object rdDepartment
	) {
		return findByProperty(RD_DEPARTMENT, rdDepartment
		);
	}
	
	public List findByRdSpeciality(Object rdSpeciality
	) {
		return findByProperty(RD_SPECIALITY, rdSpeciality
		);
	}
	
	public List findByRdGrade(Object rdGrade
	) {
		return findByProperty(RD_GRADE, rdGrade
		);
	}
	
	public List findByRdTelephone(Object rdTelephone
	) {
		return findByProperty(RD_TELEPHONE, rdTelephone
		);
	}
	
	public List findByRdMobilephone(Object rdMobilephone
	) {
		return findByProperty(RD_MOBILEPHONE, rdMobilephone
		);
	}
	
	public List findByRdAddress(Object rdAddress
	) {
		return findByProperty(RD_ADDRESS, rdAddress
		);
	}
	
	public List findByRdDateBorn(Object rdDateBorn
	) {
		return findByProperty(RD_DATE_BORN, rdDateBorn
		);
	}
	
	public List findByRdDateRploss(Object rdDateRploss
	) {
		return findByProperty(RD_DATE_RPLOSS, rdDateRploss
		);
	}
	
	public List findByRdSuspAs(Object rdSuspAs
	) {
		return findByProperty(RD_SUSP_AS, rdSuspAs
		);
	}
	
	public List findByRdDateUseless(Object rdDateUseless
	) {
		return findByProperty(RD_DATE_USELESS, rdDateUseless
		);
	}
	
	public List findByRdDateGet(Object rdDateGet
	) {
		return findByProperty(RD_DATE_GET, rdDateGet
		);
	}
	
	public List findByRdMomeny(Object rdMomeny
	) {
		return findByProperty(RD_MOMENY, rdMomeny
		);
	}
	
	public List findByRdEmail(Object rdEmail
	) {
		return findByProperty(RD_EMAIL, rdEmail
		);
	}
	
	public List findByRdPassword(Object rdPassword
	) {
		return findByProperty(RD_PASSWORD, rdPassword
		);
	}
	
	public List findByIll(Object ill
	) {
		return findByProperty(ILL, ill
		);
	}
	
	public List findByRdPosition(Object rdPosition
	) {
		return findByProperty(RD_POSITION, rdPosition
		);
	}
	
	public List findByRdJjdate(Object rdJjdate
	) {
		return findByProperty(RD_JJDATE, rdJjdate
		);
	}
	
	public List findByRdDeposit(Object rdDeposit
	) {
		return findByProperty(RD_DEPOSIT, rdDeposit
		);
	}
	
	public List findByPara(Object para
	) {
		return findByProperty(PARA, para
		);
	}
	
	public List findByRdClassnum(Object rdClassnum
	) {
		return findByProperty(RD_CLASSNUM, rdClassnum
		);
	}
	
	public List findByRdBodycode(Object rdBodycode
	) {
		return findByProperty(RD_BODYCODE, rdBodycode
		);
	}
	
	public List findByZx(Object zx
	) {
		return findByProperty(ZX, zx
		);
	}
	
	public List findByXgh(Object xgh
	) {
		return findByProperty(XGH, xgh
		);
	}
	
	public List findByCardcode(Object cardcode
	) {
		return findByProperty(CARDCODE, cardcode
		);
	}
	
	public List findByPercode(Object percode
	) {
		return findByProperty(PERCODE, percode
		);
	}
	

	public List findAll() {
		log.debug("finding all Reader instances");
		try {
			String queryString = "from Reader";
	         Query queryObject = getSession().createQuery(queryString);
			 return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}*/
	
    public Reader merge(Reader detachedInstance) {
        log.debug("merging Reader instance");
        try {
            Reader result = (Reader) getSession()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(Reader instance) {
        log.debug("attaching dirty Reader instance");
        try {
            getSession().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(Reader instance) {
        log.debug("attaching clean Reader instance");
        try {
            getSession().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    /* *
     * @author Kelvin Pan
     * */
	public List findAll(int start,int limit) {
		log.debug("finding all Reader instances with start and limit");
		try {
			String queryString = "from Reader";
	         Query queryObject = getSession().createQuery(queryString);
	         queryObject.setFirstResult(start);
	         queryObject.setMaxResults(limit);
			 return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	/* *
	 * @author Kelvin Pan
	 * */
	public int getRowCount() {
		log.debug("get the count of Reader instances");
		try {
			String queryString = "select count(*)from Reader" ;
	         Query queryObject = getSession().createQuery(queryString);
	         return ((Integer) queryObject.iterate().next()).intValue();
		} catch (RuntimeException re) {
			log.error("get count failed", re);
			throw re;
		}
	}
    
}